package cn.gson.crm.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.gson.crm.model.dao.LLJ_ConnectorDao;
import cn.gson.crm.model.dao.LLJ_ConnectorStateDao;
import cn.gson.crm.model.dao.LLJ_Connector_selectDao;
import cn.gson.crm.model.pojo.ConnectorSelect;
import cn.gson.crm.model.pojo.ConnectorState;
import cn.gson.crm.model.pojo.ConnectorStateDetail;
import cn.gson.crm.model.pojoVo.ConnectorSelectVo;
import cn.gson.crm.model.pojoVo.ConnectorStateDetailVo;
import cn.gson.crm.model.pojoVo.ConnectorStateVo;

@Service
@Transactional(rollbackFor = Exception.class)
public class LLJ_ConnectorService {
	@Autowired
	private LLJ_ConnectorDao llj_ConnectorDao;
	@Autowired
	private LLJ_Connector_selectDao llj_Connector_selectDao;
	@Autowired
	private LLJ_ConnectorStateDao llj_ConnectorStateDao;;
	
	    
	/**
	 * ConnectorState表
	 * 添加一条接件状况数据
	 */
	public String addOneData(String connectorName){
		System.out.println("输出接件名字："+connectorName);  
		try {
			ConnectorState cc=new ConnectorState();   
			cc.setConnectorName(connectorName);
			llj_ConnectorDao.save(cc);
			return "success";
			
		} catch (Exception e) {    
			return "failed";  
		}   
	}
	
	/**
	 * ConnectorState表
	 * 删除一条接件状况数据
	 */
	public boolean deleteData(Long id){
		boolean judge=false;
		try {
			ConnectorState cc=new ConnectorState();
			cc=llj_ConnectorDao.findOne(id);
			llj_ConnectorDao.delete(cc);
			judge=true;
		} catch (Exception e) {
			judge=false;
		}  
		
		return judge;  		
	}   
	
	/**
	 * ConnectorState表
	 * 查询接件状况数据
	 */
	public List<ConnectorState> findAll_connectorData(){
		return llj_ConnectorDao.searchAll();       
		
	}
	
	/**
	 * ConnectorSelect表
	 * 查询接件详情类型选择表数据   
	 */   
	public List<ConnectorSelect> findAll_ConnectorSelect(){        
		return llj_Connector_selectDao.searchAll();      
	}  
	
	/**
	 * ConnectorState表
	 * ConnectorStateDetail表
	 * ConnectorSelect表
	 * 通过'接件状况表'id查询'接件状况详情表'和'接件详情类型选择表'数据
	 */
	public ConnectorStateVo getNeedData(Long connectorId){
		ConnectorStateVo stateVo = new ConnectorStateVo();   
		List<ConnectorStateDetailVo> detailsVoList = new ArrayList<ConnectorStateDetailVo>();
		ConnectorStateDetailVo detailsVo;
		ConnectorSelectVo selectVo;
		try {
			ConnectorState statePo = llj_ConnectorStateDao.findOne(connectorId);
			stateVo.setConnectorDetailId(statePo.getConnectorDetailId());
			stateVo.setConnectorId(statePo.getConnectorId());
			stateVo.setConnectorName(statePo.getConnectorName());
			for(ConnectorStateDetail detailsPo: statePo.getConnectorStateDetails()){
				detailsVo = new ConnectorStateDetailVo();
				detailsVo.setConnectorDetailId(detailsPo.getConnectorDetailId());
				detailsVo.setConnectorDetailName(detailsPo.getConnectorDetailName());
				detailsVo.setConnectorSelectGetvalue(detailsPo.getConnectorSelectGetvalue());
				detailsVo.setConnectorSelectSetvalue(detailsPo.getConnectorSelectSetvalue());
				
				ConnectorSelect selectPo = detailsPo.getConnectorSelect();
				selectVo = new ConnectorSelectVo();
				selectVo.setConnectorSelectGetvalue(selectPo.getConnectorSelectGetvalue());
				selectVo.setConnectorSelectId(selectPo.getConnectorSelectId());
				
				detailsVo.setConnectorSelect(selectVo);     
				detailsVoList.add(detailsVo);
			}
			stateVo.setConnectorStateDetails(detailsVoList);
			return stateVo;  
		} catch (Exception e) {
			e.printStackTrace();
			throw new ServiceException(e); 
		}  
		
	}
	
	
	        
	   

}
